#include "StdAfx.h"
#include "ClientWriteStrategy.h"
#include "RdcTransport.h"

void rdc_transport::WriteImplStrategy::Write(Buffer* buffer)
{
    boost::unique_lock<boost::mutex> l(m_writeLock);
    if (m_cancelled)
        throw std::runtime_error(std::string(__FUNCTION__) + " Write in terminated state");
    
    m_write->Write(buffer);

    m_writeComplete.wait(l);
    if (m_cancelled)
        throw std::runtime_error(std::string(__FUNCTION__) + " Write is terminated");
}

void rdc_transport::WriteImplStrategy::WriteComplete(LPVOID, UINT32, UINT32, UINT32)
{
    boost::unique_lock<boost::mutex> l(m_writeLock);

    m_cancelled = false;
    m_writeComplete.notify_all();
}

void rdc_transport::WriteImplStrategy::WriteCancelled(LPVOID, UINT32, UINT32, UINT32)
{
    boost::unique_lock<boost::mutex> l(m_writeLock);

    m_cancelled = true;
    m_writeComplete.notify_all();
}

rdc_transport::WriteImplStrategy::WriteImplStrategy()
: m_cancelled(false)
{
}

void rdc_transport::WriteImplStrategy::SetInternalWriter( IVirtChannel* write )
{
    m_write.reset(write);
}